﻿/*
   Advanced Flash Engine in ActionScript 3.
   Copyright (C) 2009  Dean Gardiner, Taylor Lodge

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.winmastergames.lolgame.enemys {
	import flash.display.*;
	import flash.events.*;
	import com.winmastergames.lolgame.consoleParser;
	import com.winmastergames.lolgame.mapDefault;
	import com.winmastergames.lolgame.powerups.*;
	import com.winmastergames.lolgame.engine;
	import com.winmastergames.lolgame.globals;
	import com.winmastergames.lolgame.player;
	import com.winmastergames.gameassets.achievements;
	import com.adventuresinactionscript.collisions.HitTest;
	import com.anotherearlymorning.PixelPerfect.*;
	import flash.geom.Rectangle;
	import com.winmastergames.lolgame.gameUtils;
	import com.winmastergames.gameassets.xperience;
	import com.winmastergames.lolgame.powerups.powerUpSpawner;

	//Main enemy class used to set variables unique to that enemy on the stage
	public class base extends MovieClip {
		//This is used to check if the enemy is already dead when killing it
		public var killed:Boolean = false;
		//This is how much health the enemy spawns with also set when the enemy get's spawned. Setting this to - will also kill the enemy
		public var health:Number = 100;
		//This is how much health the enemy has remaining.
		public var healthRem:Number = 100;
		//This is what acctually kills off the enemy. Setting it to true will kill the enemy
		public var alive:Boolean = true;
		//This is the stageref for spawning the enemy
		var Mstage:mapDefault;
		//This is the powerups array which is used when picking a random powerup to spawn on enemy death
		var powerUpArray:Array = ["ammo", "insta", "damage", "inv", "repair"];
		//Holds radian's during the roatain conversion
		var radian:Number;
		//Disables movement if false
		var canMove:Boolean = true;
		//If false enemy wont change to another path node
		//Used so that the enemy doesn't spam pathnodes
		var switchPathNode:Boolean = true;
		//Store's the X position of the closest pathnode
		var closestPathnodeX:Number = 999999999;
		//Store's the Y position of the closest pathnode
		var closestPathnodeY:Number = 999999999;
		//Used in the closestPathnode if statement to compare and find the closest pathnode
		var closestPathnodeDis:Number = 99999999;
		var lastPathNodeID:Number;
		//Captures the id of the last pathnode so we disable switching to it
		var id:Number = -1;
		//Captures the id of the second to last pathnode so we disable switching to it
		var id2:Number = -1;
		//This goes up by 1 every time we run the closestPathnode function
		//Used to detect which id to set
		var timesRun:Number = 0;
		//Get's set to true when we hit a pathnode so we can disable hitPathnode spam
		var hitThisSession:Boolean;
		//This is the enemy spawn function, xLoc and yLoc is the enemy's spawn x and y,Stageref,And health
		var tickTime:int = 0;
		var isTouchingWall:Boolean = false;
		var ys:Number;
		var xs:Number;
		public static var totalPowerUp:Number;
		public static var objectPaused:Boolean = false;

		public function base(xLoc:Number, yLoc:Number, stageRef:mapDefault, enHealth:Number){
			//Set's health value is gotten from the function
			health = enHealth;
			//Makes sure the health remaining is what the health is
			healthRem = enHealth;
			//Set's mstage to the stageref for later use
			Mstage = stageRef;
			//Add's the enemy to the stage.
			stageRef.addChild(this);
			//Set's the x coord to what was set in the function
			x = xLoc;
			//Set's the y coord to what was set in the function
			y = yLoc;
			//Add's the loop event listner
			addEventListener(Event.ENTER_FRAME, loop);
		}

		//This spawns the powerup on enemy death if chanceOfDrop is greater then 79
		public function spawnPowerUp(xLoc:Number, yLoc:Number){
			//Creates a random number between 0 and 5 and then rounds it.
			var powerUpRandom:Number = Math.round(Math.random() * 5);
			powerUpSpawner.spawnPowerUp(xLoc, yLoc, powerUpArray[powerUpRandom]);
		}

		//Killself function called on death of enemy
		public function killSelf(type:String){
			if (type == "player"){
				xperience.XP++;
				achievements.st_achievementsArray["monsterKills"].total++;
				achievements.st_achievementsArray["monsterKills"].poppedUp = false;
				player.money += Math.round(Math.random() * (5 - 2) + 2);
			}
			var dropX:Number = x;
			//Grabs the enemy's x position so we can drop a powerup there.
			var dropY:Number = y;
			//Grabs the enemy's y position so we can drop a powerup there.
			var chanceOfDrop:Number = Math.round(Math.random() * (100 - 1) + 1);
			//Generates a random number between 1 and 100 and then rounds it.
			//If the enemy has been killed
			if (killed == false){
				if (player.weaponType == "Pistol"){
					//achievements.setAchievements("pistol", "++");
				}
				alive = false;
				//Kills it
				Mstage.removeChild(this);
				//Removes it from the stage
				//Only drops powerup if chance of drop is greater then 79 
				if (chanceOfDrop >= 90 && globals.powerupsThisRound < globals.maxPowerups){
					globals.powerupsThisRound++;
					//Add's one to powerUpsThisRound so we can limit the amount dropped
					//UnFinished add's one to damagePowerUpsThisRound so we can limit inividual powerups
					//And finally we spawn the powerup at the trapped x,y coord's of the enemy
					spawnPowerUp(dropX, dropY);
				}
			}
		}

		function convert(radians:Number):Number {
			var degrees = radians * (180 / Math.PI);
			return degrees;
		}

		public function loop(e:Event) {
			objectPaused = engine.objectsPaused;
			if (objectPaused == true)
				this.stop();
			else
				this.play();
			
			if (objectPaused == false){
				var adj:Number = engine.st_playerTarget.y - y;
				var opp:Number = engine.st_playerTarget.x - x;
				var rot:Number = Math.atan2(adj, opp);
				rotation = convert(rot);
				if (globals.getDistance(x, y, engine.st_playerTarget.x, engine.st_playerTarget.y) < 50)
				{
					if (this.hitTestObject(engine.st_player_mc)) {
						canMove = false;
						if (engine.god == false){
							player.playerHealth -= 1;
							player.regen = false;
							player.healthRegen.stop();
							player.healthRegen.reset();
						}
					}
				}
				else {
					canMove = true;
					/*if (PixelPerfectCollisionDetection.isColliding(this, engine.st_level_mc.walls, engine.st_level_mc, true)){
						isTouchingWall = true;
						canMove = false;
					} else {
						isTouchingWall = false;
						canMove = true;
					}*/
				}
				if (canMove == true){
					xs = 2 * Math.cos((rotation * Math.PI) / 180);
					ys = 2 * Math.sin((rotation * Math.PI) / 180);
					x += xs;
					y += ys;
				} else if (isTouchingWall == true){
					xs = 4.5 * Math.cos((rotation * Math.PI) / 180);
					ys = 4.5 * Math.sin((rotation * Math.PI) / 180);
					if (x - engine.st_playerTarget.x > 0)
						x += xs + 5;
					else
						x -= xs + 5
					if (y - engine.st_playerTarget.y > 0)
						y += ys + 5;
					else
						x -= ys + 5;
					x -= xs;
					y -= ys;
				}
//				if (hitTestObject(engine.barrierHand.barriers[0]))
				//{
				//	engine.barrierHand.barriers[0].damageBarrier(0.01);
				//}
			}
		}
	}
}